<?php

defined('SYSPATH') or die('No direct script access.');

/**
 * 活动转发
 * @version $Id: transmit.php 4 2011-12-15 09:28:04Z 周永隆 $
 */
class CLI_Activity_Transmit extends CLI
{

    protected $_run_time = 60; // 估计运行间隔

    public function main() {
        try{
            $bench_start = microtime(TRUE);
            
            $update_ids = array(); // 记录已开始活动ID
            $update_ids[] = 0;
            $select = DB::select('id', 'retweeted_type', 'start_time', 'status')
                ->from('activity_transmit')
                ->where('end_time', '>', time())
                ->where('status', '>', '-1')
                ->fetch_all();

            foreach ($select as $row)
            {
                if ($row['start_time'] < time() AND $row['status'] == 2)
                {
                    $update_ids[] = $row['id'];
                    $t_cache = Cache::instance()->get('activity_transmit_'.$row['id']);
                    if ($t_cache)
                    {
                        $t_cache['status'] = 1;
                        Cache::instance()->set('activity_transmit_'.$row['id'], $t_cache, 3600); // 写入缓存
                    }
                }
                
                echo '[id-', $row['id'],']',"\n";
                if ($row['retweeted_type'] == 'sina_weibo')
                {
                    Activity_Transmit_Sina_Weibo::run($row['id']);
                }
            }

            // 更新已开始活动状态字段
            DB::update('activity_transmit')
                ->set(array('status' => 1, 'modified' => time()))
                ->where('id', 'IN', $update_ids)
                ->execute();
            
            // 需要更已结束状态字段
            $update_ids = array(); // 记录已结束活动ID
            $update_ids[] = 0;
            $select =DB::select('id')
                ->from('activity_transmit')
                ->where_open()
                ->where('end_time', '<', time())
                ->or_where('payed', '=', DB::expr('`old_stock`'))
                ->where_close()
                ->where('status', 'IN', array(1,2))
                ->fetch_all();
            foreach ($select as $row)
            {
                $update_ids[] = $row['id'];
                $t_cache = Cache::instance()->get('activity_transmit_'.$row['id']);
                if ($t_cache)
                {
                    $t_cache['status'] = 3;
                    Cache::instance()->set('activity_transmit_'.$row['id'], $t_cache, 3600); // 写入缓存
                }
            }

            DB::update('activity_transmit')
                ->set(array('status' => 3, 'modified' => time()))
                ->where('id', 'IN', $update_ids)
                ->execute();

            $bench_end = microtime(TRUE);
            $eclipse_time = round($bench_end - $bench_start, 3);
            $log = "更新活动转发数据 执行用时  {$eclipse_time} 秒.".date('Y-m-d H:i:s');
            CLI::output("\n" . $log);
        }
        catch(Exception $e)
        {
            DB::delete('cli_running')
                ->where('class', '=', get_class($this))
                ->execute();
            CLI::output("\n" . $e->getMessage());
        }
    }

    /**
     * 构造方法
     *
     * @param  CLI_Args  $args
     * @return void
     */
    public function __construct(CLI_Args $args = NULL)
    {

        $this->args = $args;
        if(DB::select('addtime')
            ->from('cli_running')
            ->where('class', '=', get_class($this))
            ->where('addtime', '>', time() - $this->_run_time)
            ->fetch_one())
        {
            CLI::output("\n 此分析器正在运行中");
            die();
        }
        else
        {
            $this->_runed = true;
            DB::insert('cli_running', array('class', 'addtime'))
                ->values(array(get_class($this), time()))
                ->execute();
        }
    }

    /**
     * 删除运行状态
     */
    public function  __destruct()
    {
        if($this->_runed)
        {
            DB::delete('cli_running')->where('class', '=', get_class($this))->execute();
        }
    }

}